<template>
    <div class="school-student-container">
        <el-dialog title="新增学生信息" v-model="isShowDialog" >
            <el-form ref="addStudentFormRef" :model="formData" :rules="formRules" label-width="80px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="名称" prop="name">
                            <el-input v-model="formData.name" placeholder="请输入名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="备注" prop="remark">
                            <el-input type="textarea" v-model="formData.remark" placeholder="请输入备注" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="状态" prop="status">
                            <el-select v-model="formData.status" placeholder="请选择状态" class="w100">
                                <el-option key="key1" label="关键值1" value="key1"></el-option>
                                <el-option key="key2" label="关键值2" value="key3"></el-option>
                                <el-option key="key3" label="关键值3" value="key3"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="联系电话" prop="phone">
                            <el-input v-model="formData.phone" placeholder="请输入联系电话" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="排序" prop="sort">
                            <el-input-number v-model="formData.sort" :min="-9999" :max="9999" clearable class="w100"></el-input-number>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="父级编号" prop="parentId">
                            <el-input v-model="formData.parentId" placeholder="请输入父级编号" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="祖级序列" prop="ancestors">
                            <el-input v-model="formData.ancestors" placeholder="请输入祖级序列" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onCancel">取 消</el-button>
                    <el-button type="primary" @click="onSubmit">保 存</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script lang="ts">
import { reactive, toRefs, ref } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { addStudent } from '/@/api/school/student/index';
export default {
    name: 'school/student/add',
    setup(props, context) {
        const addStudentFormRef = ref();
        const state = reactive({
            isShowDialog: false,
            formData: {
            },
            formRules : {
                name: { required: true, message: '名称不能为空', trigger: 'blur' },
            }
        });

        
        // 打开弹窗
        const openDialog = (row?: object) => {
            state.isShowDialog = true;
        };
        // 关闭弹窗
        const closeDialog = (row?: object) => {
            state.isShowDialog = false;
            for(var key in state.formData) {
                state.formData[key] = undefined;
            }
            addStudentFormRef.value.resetFields();
        };
        // 取消
        const onCancel = () => {
            closeDialog();
        };
        // 新增
        const onSubmit = () => {
            addStudentFormRef.value.validate(valid => {
                if(valid){
                    let params = {
                        name: state.formData.name,
                        remark: state.formData.remark,
                        status: state.formData.status,
                        phone: state.formData.phone,
                        sort: state.formData.sort,
                        parentId: state.formData.parentId,
                        ancestors: state.formData.ancestors,
                    }
                    addStudent(params).then((res) => {
                        if(res.code === 0){
                            ElMessage.success('保存成功');
                            context.emit('submit');
                            closeDialog();
                        }
                    });
                }
            });
        };
        return {
            addStudentFormRef,
            openDialog,
            closeDialog,
            onCancel,
            onSubmit,
            ...toRefs(state),
        };
    },
};
</script>